import Vue from 'vue'
import { login } from '@/api/user'
import { checkAdminInfo } from '@/api/navBar'
import { getToken, setToken, removeToken } from '@/utils/auth'
import qs from 'qs'
// import { resetRouter } from '@/router'
const getDefaultState = () => {
    return {
        token: getToken(),
        name: '',
        avatar: '',
        role_id: ''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_ROLE_ID: (state, role_id) => {
        state.role_id = role_id
    }
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        const { username, password } = userInfo
        console.log(userInfo)
        return new Promise((resolve, reject) => {
            login(qs.stringify({ username: username.trim(), password: password })).then(response => {
                console.log(response)
                if (response.code === 0) {
                    Vue.prototype.$message.error('用户名或者密码错误！')
                    reject()
                }
                const data = response
                console.log(data)
                commit('SET_TOKEN', data.result.token)
                setToken(data.result.token)
                window.localStorage.setItem('username', JSON.stringify(username))
                window.localStorage.setItem('loginData', JSON.stringify(data))
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    async getInfo({ commit, state }) {
        console.log(state)
        let username = JSON.parse(localStorage.getItem('username'))
        let info = JSON.parse(localStorage.getItem('loginData'))
            // console.log(info)
        const res = await checkAdminInfo(info.result.user_info.id)
        console.log(res)
        return new Promise((resolve, reject) => {
            let data = {
                // roles: ['admin'],
                // name: '小爱',
                avatar: 'https://cloud.xiaoxingbobo.top/nongzhibang/20210429/1107491622257669573'
            }
            if (res.result[0].avatarUrl !== 0) {
                data.avatar = res.result[0].avatarUrl
            }
            data.name = username
            data.role_id = info.result.user_info.role_id
            commit('SET_ROLE_ID', data.role_id)
            commit('SET_NAME', data.name)
            commit('SET_AVATAR', data.avatar)
            resolve(data)
                // getInfo(state.token).then(response => {
                //     const { data } = response

            //     if (!data) {
            //         return reject('Verification failed, please Login again.')
            //     }

            //     const { name, avatar } = data

            //     commit('SET_NAME', name)
            //     commit('SET_AVATAR', avatar)
            //     resolve(data)
            // }).catch(error => {
            //     reject(error)
            // })
        })
    },

    // user logout
    logout({ commit, state }) {
        // console.log(resetToken)  
        return new Promise((resolve, reject) => {
            removeToken()
            commit('RESET_STATE')
            resolve()
                // logout(state.token).then(() => {
                //     removeToken() // must remove  token  first
                //     resetRouter()
                //     commit('RESET_STATE')
                //     resolve()
                // }).catch(error => {
                //     reject(error)
                // })
        })
    },

    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}